home *** CD-ROM | disk | FTP | other *** search
- Module MainModule
-
- Sub Main()
- ' Run one of the Textxxxx procedures below by uncommenting only one statement
-
- 'TestReturn()
- 'TestCallByRef()
- 'TestArrayByVal()
- 'TestArrayByRef()
- 'TestParamArray()
- 'TestLabelProblem()
- 'TestFindWindow()
- 'TestVBFunctions()
-
- ' These statements are usuful when running inside Visual Studio.NET
- Console.WriteLine("")
- Console.WriteLine(">>> Press Enter to terminate the program <<<")
- Console.ReadLine()
- End Sub
-
- ' this procedure tests the Return performance compared to traditional
- ' way to return from a procedure
-
- Sub TestReturn()
- Dim start As Date
- Dim i As Integer
- Dim res As Integer
-
- start = Now
- For i = -10000000 To 10000000
- res = TestFunction_One(i)
- Next
- Console.WriteLine("Using local variable: {0} secs.", Now.Subtract(start))
-
- start = Now
- For i = -10000000 To 10000000
- res = TestFunction_Two(i)
- Next
- Console.WriteLine("Using Return statement: {0} secs.", Now.Subtract(start))
- End Sub
-
- ' auxiliary functions
-
- Function TestFunction_One(ByVal x As Integer) As Integer
- If x > 0 Then
- TestFunction_One = 1
- Else
- TestFunction_One = -1
- End If
- End Function
-
- Function TestFunction_Two(ByVal x As Integer) As Integer
- If x > 0 Then
- Return 1
- Else
- Return -1
- End If
- End Function
-
- ' this procedure demonstrates that array passed ByVal can be
- ' modified by the called procedure
-
- Sub TestArrayByVal()
- Dim anArray() As Integer = {0, 1, 2, 3, 4}
- ' Pass the array by value to a procedure.
- Call ArrayProcByval(anArray)
- ' Prove that the array has been modified.
- Console.WriteLine(anArray(3)) ' => 300
- End Sub
-
- ' A procedure that modifies its array argument's elements
- Sub ArrayProcByval(ByVal arr() As Integer)
- Dim i As Integer
- For i = 0 To UBound(arr)
- arr(i) = arr(i) * 100
- Next
- End Sub
-
- ' this procedure shows the difference in passing an array byval or byref
-
- Sub TestArrayByRef()
- Dim byvalArray(10) As Integer
- Dim byrefArray(10) As Integer
- ' Pass both arrays to the procedure.
- Call ArrayProcByRef(byvalArray, byrefArray)
- ' Check which array has been affected by the ReDim.
- Console.WriteLine(UBound(byvalArray)) ' => 10 (not modified)
- Console.WriteLine(UBound(byrefArray)) ' => 100 (modified)
- End Sub
-
- Sub ArrayProcByRef(ByVal arr() As Integer, ByRef arr2() As Integer)
- ' Change the size of both arrays.
- ReDim arr(100)
- ReDim arr2(100)
- End Sub
-
- ' this procedure tests the ParamArray keyword
-
- Sub TestParamArray()
- Console.WriteLine(Sum(3, 1, 5)) ' => 9
-
- Console.WriteLine(Sum2(3, 1, 5)) ' => 9
-
- Console.WriteLine(MinValue(3, 1, 5)) ' => 1
- End Sub
-
- ' sum all the arguments
- Function Sum(ByVal ParamArray args() As Integer) As Integer
- Dim sumResult, index As Integer
- For index = 0 To UBound(args)
- sumResult += args(index)
- Next
- Sum = sumResult
- End Function
-
- ' alternative, more concise version
- Function Sum2(ByVal ParamArray args() As Integer) As Integer
- Dim index As Integer
- For index = 0 To UBound(args)
- Sum2 = Sum2 + args(index)
- Next
- End Function
-
- ' this procedure shows that you can deal with a ParamArray argument
- ' as if it were a regular array
-
- Function MinValue(ByVal ParamArray args() As Object) As Object
- ' Sort the array, and then return its 1st element.
- System.Array.Sort(args)
- MinValue = args(0)
- End Function
-
- ' this procedure demonstrates a problem with procedures without any arguments:
-
- Sub TestLabelProblem()
- ' This code doesn't work as expected.
- WriteHello: Console.WriteLine("World") ' => World
-
- ' This code works correctly.
- WriteHello() : Console.WriteLine("World") ' => Hello World
- End Sub
-
- Sub WriteHello()
- Console.Write("Hello ")
- End Sub
-
- ' API declares
-
- Private Declare Ansi Function FindWindow Lib "user32" Alias "FindWindowA" _
- (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
- Private Declare Function MoveWindow Lib "user32" Alias "MoveWindow" _
- (ByVal hWnd As Integer, ByVal x As Integer, ByVal y As Integer, _
- ByVal nWidth As Integer, ByVal nHeight As Integer, _
- ByVal bRepaint As Integer) As Integer
-
- ' NOTE: launch Notepad on an empty document before running this code.
- Sub TestFindWindow()
- Dim hWnd As Integer = FindWindow(Nothing, "Untitled - Notepad")
- If hWnd <> 0 Then MoveWindow(hWnd, 0, 0, 600, 300, 1)
- End Sub
-
- ' this procedure tests several functions in the Microsoft.VisualBasic namespaces
-
- Sub TestVBFunctions()
- Dim s1 As String = "abc"
- Dim s2 As String = "ABC"
- Dim res As String
-
- ' Compare two strings in case-insensitive mode.
- If StrComp(s1, s2, CompareMethod.Text) = 0 Then res = "Equal"
- Console.WriteLine("res = " & res)
-
- ' This statements prints a line of 50 dashes.
- Console.WriteLine(StrDup(50, "-"))
-
- ' the GetChar method
- Console.WriteLine(GetChar("ABCDE", 2)) ' => B
-
- ' The natural logarithm of 10
- Console.WriteLine(Math.Log(10)) ' => 2.30258509299405
- ' Two ways to evaluate he decimal logarithm of 1000
- Console.WriteLine(Math.Log(1000, 10)) ' => 3
- Console.WriteLine(Math.Log10(1000)) ' => 3
-
- Console.WriteLine(Math.Min(1.5, 0.7)) ' => 0.7
- Console.WriteLine(Math.Max(99, 87)) ' => 99
-
- Console.WriteLine(Math.Floor(-1.5)) ' => -2
- Console.WriteLine(Math.Ceiling(2.5)) ' => 3
-
- Console.WriteLine(Math.IEEERemainder(2, 1.5)) ' => 0.5
-
- ' date-time functions
-
- ' Get the date two weeks from now.
- Dim newDate As Date = DateAdd(DateInterval.WeekOfYear, 2, Now())
- Console.WriteLine(newDate)
-
- ' Evaluate days left to December 31 of current year.
- Dim days As Long = DateDiff(DateInterval.Day, Today, _
- DateSerial(Year(Today), 12, 31))
- Console.WriteLine(days)
-
- ' abbreviated month name
- Console.WriteLine(MonthName(1, True)) ' => Jan
-
- ' display a msgbox
-
- Dim userChoice As MsgBoxResult
- userChoice = MsgBox("Want to run Notepad", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
- If userChoice = MsgBoxResult.Yes Then
- ' run notepad and wait until it returns, or until 10 secs. have elapsed
- Dim taskID As Long = Shell("notepad", AppWinStyle.NormalFocus, True, 10000)
- If taskID = 0 Then
- Console.WriteLine("Notepad has been closed within 10 seconds.")
- Else
- Console.WriteLine("Notepad is still running after 10 seconds.")
- End If
- End If
-
- ' read a text file
- Dim handle As Integer = FreeFile()
- ' open a file for input
- FileOpen(handle, "C:\autoexec.bat", OpenMode.Input, OpenAccess.Read)
- ' read the entire file in one operation
- Dim fileText As String = InputString(handle, CInt(LOF(handle)))
- ' close the file
- FileClose(handle)
- Console.WriteLine(fileText)
-
- ' Display the description associated to error code 9.
- Console.WriteLine(ErrorToString(9)) ' => Subscript out of Range
-
- Dim x As Integer
- Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(x)) ' => 4
-
- Console.WriteLine(SystemTypeName("Long")) ' => System.Int64
- Console.WriteLine(VbTypeName("System.Int16")) ' => Short
-
- ' The new IsReference function returns True if the argument evaluates to a reference type,
- ' or False if it is a value type:
- Dim n As Integer = 1
- Dim s As String = "ABC"
- Console.WriteLine(IsReference(n)) ' => False
- Console.WriteLine(IsReference(s)) ' => True
- End Sub
-
- End Module
-
-
-